package com.cn.kabalifemerchants.ui.login;


import com.cn.kabalifemerchants.api.Api;
import com.cn.kabalifemerchants.base.BasePresenter;
import com.cn.kabalifemerchants.callback.CacheCallBack;
import com.cn.kabalifemerchants.config.Constants;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.ui.login.bean.AddressProvinceListBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.bean.UpImgBean;
import com.cn.kabalifemerchants.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;

/**
 * Created by 晓林 on 2020/9/7.
 * function:
 */
public class CooperationPresenter extends BasePresenter<CooperationView> {
    /**
     * 上传文件。
     */
    public void onUpImgData(String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", "head");
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }
    //省
    public void onProvinceListData() {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", 1)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("省：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onProvinceListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    //市
    public void onCityListData(int pid) {
        OkGo.<AddressProvinceListBean>get(Api.API_ADD_CITY)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("pid", pid)
                .execute(new CacheCallBack<AddressProvinceListBean>() {
                    @Override
                    public void onSuccess(Response<AddressProvinceListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("市：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onCityListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    public void onUserBusinessData(int province, int city,
                                   int area, int street,
                                   String address, String service_type,
                                   String name, String phone,
                                   List<String> img, String user_name,int identity,String card,String region,String card_front,String card_back,String card_hand) {
        OkGo.<MsgBean>post(Api.API_USER_BUSINESS)
                .headers("token", LoginHelper.getLonginData().getResult())
                .params("province", province)
                .params("city", city)
                .params("area", area)
                .params("street", street)
                .params("address", address)
                .params("service_type", service_type)
                .params("name", name)
                .params("phone", phone)
                .addUrlParams("other_img[]", img)
                .params("user_name", user_name)
                .params("identity", identity)
                .params("card", card)
                .params("region", region)
                .params("card_front", card_front)
                .params("card_back", card_back)
                .params("card_hand", card_hand)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("申请：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserBusinessSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

}
